package com.ysd.crm.config;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
@RestController
public class Swagger2Config {
	@Bean
	public Docket createRestApi() {
		// 扫描控制器中 Swagger2 的注解, .apis 里面的参数表示指定包
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.ysd.crm.controller")).paths(PathSelectors.any()).build();
//				.apis(RequestHandlerSelectors.basePackage("top.tangs.shop")).paths(PathSelectors.any()).build();
	}

	// API 描述信息
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("CRM客户关系管理项目API文档")// springboot 利用 swagger 构建 api 文档
				.description("简单优雅的 restfun 风格").termsOfServiceUrl("http://crm.ysd.com").version("1.0").build();
	}
	
	@GetMapping("/api")
	public void getAPI(HttpServletResponse response) {
		try {
			response.sendRedirect("/swagger-ui.html");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
